<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      [].reduce((pre, cur, index, arr) => {}, [])
      Array.prototype.myReduce = function (callback, result) {
        const context = this
        context.forEach((item, index, arr) => {
          result = callback(result, item, index, arr)
        })
        return result
      }
      Array.prototype._reduce = function (callback) {
        let arr = this;
        let arg = [...arguments] //是为了判断有没有传入参数,不能用undefined判断，因为可能手动传入udnefined了
        let total = arg[1]
        let i = 0
        // 说明没有传入参数，且保证传入参数undefined可以接收到
        if(arg.lenght === 1) {
          i = 1
          total = arr[0]
        }
        for (i; i< arr.length;i++) {
          total = callback(total, arr[i], i, arr);
        }
        return total
      };
      let arr = [1, 2, 3, 4];
      let b = arr._reduce((pre, cur) => {
        console.log(pre);
        return pre + cur;
      });
      // console.log(b);
      // setTimeout(function () {
      //   console.log("1");
      // });

      // new Promise(function (resolve) {
      //   console.log("2");
      //   resolve();
      // }).then(function () {
      //   console.log("3");
      // });

      // console.log("4");
    </script>
  </body>
</html>
